#include "DerivationTree.h"
#include "CallInstruction.h"
#include "AutoPointer.h"

PlusNode::PlusNode() : NodeTree_CRTP<PlusNode>() { }

PlusNode::PlusNode(const AutoPointer<NodeTree>& _left, const AutoPointer<NodeTree>& _right) : NodeTree_CRTP<PlusNode>(), left(_left), right(_right) { }

PlusNode::PlusNode(const PlusNode& copy) : NodeTree_CRTP<PlusNode>(copy), left(copy.left), right(copy.right) { }

bool PlusNode::execute(Pointer<LeonardVirtualMachine> LVM) {
	left->execute(LVM);
	right->execute(LVM);
	AutoPointer<TreeExpression::Expression> second = LVM->top();
	LVM->pop();
	AutoPointer<TreeExpression::Expression> first = LVM->top() ;
	LVM->pop();
	first->addLeaf(second);
	TreeExpression::CallInstruction* plusCall = new TreeExpression::CallInstruction("operator+");
	plusCall->setSon(first);
	AutoPointer<TreeExpression::Expression> ris(plusCall);
	LVM->push(ris);
	return true;
}
